package com.aaa.config;

import com.aaa.entity.Teacher;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @author Administrator
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2022/8/23 11:06
 * @description：这辈子第一个spring配置类
 * @modified By：
 * @version:
 */
@Configuration
/**
 * 扫描带注解的所有类
 */
@ComponentScan(basePackages = "com.aaa")
/**
 * 加载配置文件
 */
@PropertySource("db.properties")
public class SpringConfig {
    @Value("${my.url}")
    private String url;
    @Value("${my.driver}")
    private String driverClassName;
    @Value("${my.username}")
    private String name;
    @Value("${my.password}")
    private String password;

    /**
     * 在配置类中实例化bean
     *   @Bean  标记此方法的返回值为一个bean
     *name = "teacher"  是bean的名字，不写默认的是方法名
     * @return
     */
   // @Bean(name = "xxxteacher")
    @Bean
    public Teacher oooteacher(){
        Teacher teacher = new Teacher();
        teacher.setId(10000);
        teacher.setName("陈建");
       return teacher;
    }
    @Bean
    public BasicDataSource dataSource(){
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(this.driverClassName);
        dataSource.setUrl(this.url);
        dataSource.setUsername(this.name);
        dataSource.setPassword(this.password);
        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(){
        BasicDataSource basicDataSource = dataSource();
        JdbcTemplate jdbcTemplate = new JdbcTemplate(basicDataSource);
       // jdbcTemplate.setDataSource();
        return jdbcTemplate;
    }

}
